/**
 * @author:cavion(曹仁道)
 * @email:caorendao187@163.com
 * 2016年9月16日 
 */
package org.rk.core.jdbc.dao.util;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.rk.core.common.bean.OrderBean;
import org.rk.core.common.bean.PageData;
import org.rk.core.common.util.RkObjectUtil;
import org.rk.core.common.util.RkStrUtil;

/**
 * @author:cavion
 * @描述：参数工具类
 * 2016年9月16日 下午7:27:33
 */
public class ParamMap implements Serializable{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -5756091205999881347L;
	private String condiMode;//查询条件模型，>/</=...
	private String condiType;//添加查询类型,and/or/order/limit
	private String paramName;//参数名
	private Object paramValue;//参数值
	
	
	public ParamMap(){}
	public ParamMap(String paramName,Object paramValue){
		this.condiType=Condition.condi_type_and;
		if(paramValue instanceof String){
			if(((String) paramValue).indexOf("%")>=0){//只要有百分号，认为是模糊查询
				this.condiMode=Condition.condi_mode_like;
			}else{
				this.condiMode=Condition.condi_mode_eq;
			}
		}else{
			this.condiMode=Condition.condi_mode_eq;
		}
		
		this.paramName=paramName;
		this.paramValue=paramValue;
	}
	public ParamMap(String condiType,String paramName,Object paramValue){
		this.condiType=RkStrUtil.isEmpty(condiType)?Condition.condi_type_and:condiType;
		if(paramValue instanceof String){
			if(((String) paramValue).indexOf("%")>=0){//只要有百分号，认为是模糊查询
				this.condiMode=Condition.condi_mode_like;
			}else{
				this.condiMode=Condition.condi_mode_eq;
			}
		}else{
			this.condiMode=Condition.condi_mode_eq;
		}
		this.paramName=paramName;
		this.paramValue=paramValue;
	}
	public ParamMap(String condiMode,String condiType,String paramName,Object paramValue){
		this.condiType=RkStrUtil.isEmpty(condiType)?Condition.condi_type_and:condiType;
		this.condiMode=RkStrUtil.isEmpty(condiMode)?Condition.condi_mode_eq:condiMode;
		this.paramName=paramName;
		this.paramValue=paramValue;
	}
	
	/**
	 * 
	 * 2016年9月16日 下午9:14:40
	 * @author:cavion
	 * @描述：将参数转换为map
	 * @param paramMapList
	 * @return
	 */
	public static Map<String,Object> genParams(List<ParamMap> paramMapList){
		Map<String,Object> param=new HashMap<String, Object>();
		for (ParamMap paramMap:paramMapList) {
			if(!paramMap.getCondiType().equals(Condition.condi_type_limit)&&!paramMap.getCondiType().equals(Condition.condi_type_order)
					&&!paramMap.getCondiType().equals(Condition.condi_type_group)){
				param.put(paramMap.getParamName(), paramMap.getParamValue());
			}
		}
		return param;
	}
	
	public static List<ParamMap> addPageToParamMap(List<ParamMap> paramMapList,PageData page){
		int cur=page.getStart();
		int num=page.getLength();
		if(num==0){
			num=10;//给一个默认值
		}
		paramMapList.add(new ParamMap(Condition.condi_type_limit, Condition.limit_start_param_name, cur));
		paramMapList.add(new ParamMap(Condition.condi_type_limit, Condition.limit_num_param_name, num));
		return paramMapList;
	}
	
	public static List<ParamMap> addOrderToParamMap(List<ParamMap> paramMapList,List<OrderBean> orderList){
		if(RkObjectUtil.isEmpty(orderList)||orderList.size()<1){
			return paramMapList;
		}
		for(OrderBean orderBean:orderList){
			paramMapList.add(new ParamMap(orderBean.getOrderType(),Condition.condi_type_order, orderBean.getOrderField(),orderBean.getOrderType()));
		}
		return paramMapList;
	}
	
	public static List<ParamMap> addPageAndOrderToParamMap(List<ParamMap> paramMapList,PageData page,List<OrderBean> orderList){
		paramMapList=addPageToParamMap(paramMapList,page);
		paramMapList=addOrderToParamMap(paramMapList,orderList);
		return paramMapList;
	}
	
	public String getCondiType() {
		return condiType;
	}
	public void setCondiType(String condiType) {
		this.condiType = condiType;
	}
	public String getParamName() {
		return paramName;
	}
	public void setParamName(String paramName) {
		this.paramName = paramName;
	}
	public Object getParamValue() {
		return paramValue;
	}
	public void setParamValue(Object paramValue) {
		this.paramValue = paramValue;
	}
	public String getCondiMode() {
		return condiMode;
	}
	public void setCondiMode(String condiMode) {
		this.condiMode = condiMode;
	}

}
